package com.ddwl.message.getui.template;

import com.gexin.rp.sdk.base.notify.Notify;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.template.StartActivityTemplate;
import com.ddwl.message.constant.MessageConstant;
import com.gexin.rp.sdk.dto.GtReq;
import com.gexin.rp.sdk.template.TransmissionTemplate;

/**
 * 推送模板
 *
 * @author zhangwf
 * @see
 * @since 2019-07-09
 */
public class PushTemplate {

    /**
     * 透传消息模版,透传消息是指消息传递到客户端只有消息内容，展现形式由客户端自行定义。客户端可自定义通知的展现形式，也可自定义通知到达之后的动作，或者不做任何展现。
     * @return
     */
    public static TransmissionTemplate getTransmissionTemplate(String msgTitle, String msgText, String transContent) {
        TransmissionTemplate template = new TransmissionTemplate();
        //"{\"title\": \""+msgTitle+"\",\"content\": \""+msgText+"\",\"payload\": \""+transContent+"\"}"
        template.setTransmissionContent("{\"title\": \""+msgTitle+"\",\"content\": \""+msgText+"\",\"payload\": \""+transContent+"\"}");
        template.setTransmissionType(2);
        Notify notify = new Notify();
        notify.setTitle(msgTitle);
        notify.setContent(msgText);
        notify.setIntent(String.format(MessageConstant.intent,msgTitle,msgText,transContent));
        notify.setType(GtReq.NotifyInfo.Type._intent);
        template.set3rdNotifyInfo(notify);
        template.setAPNInfo(getAPNPayload(msgTitle,msgText,transContent));
        return template;
    }

    public static StartActivityTemplate getStartActivityTemplate(String msgTitle, String msgText,String transContent) {
        StartActivityTemplate template = new StartActivityTemplate();
        //设置展示样式
        template.setStyle(PushStyle.getStyle0(msgTitle, msgText));
        template.setIntent(String.format(MessageConstant.intent,msgTitle,msgText,transContent));
        template.setAPNInfo(getAPNPayload(msgTitle,msgText,transContent));
        return template;
    }

    private static APNPayload getAPNPayload(String msgTitle,String msgText,String transContent) {
        APNPayload payload = new APNPayload();
        //在已有数字基础上加1显示，设置为-1时，在已有数字上减1显示，设置为数字时，显示指定数字
        payload.setAutoBadge("0");
        payload.setContentAvailable(0);
        //ios 12.0 以上可以使用 Dictionary 类型的 sound
        payload.setSound("default");
        payload.setCategory("default");
        payload.addCustomMsg("title", msgTitle);
        payload.addCustomMsg("content", msgText);
        payload.addCustomMsg("payload", transContent);
        //字典模式使用APNPayload.DictionaryAlertMsg
        payload.setAlertMsg(getDictionaryAlertMsg(msgTitle,msgText));
        return payload;
    }

    private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String msgTitle,String msgText) {
        APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
        //消息内容
        alertMsg.setTitle(msgTitle);
        alertMsg.setBody(msgText);
        alertMsg.setActionLocKey("ActionLockey");
        alertMsg.setLocKey("LocKey");
        alertMsg.addLocArg("loc-args");
        alertMsg.setLaunchImage("launch-image");
        alertMsg.setTitleLocKey("TitleLocKey");
        alertMsg.addTitleLocArg("TitleLocArg");
        // iOS8.2以上版本支持
//        alertMsg.setSubtitle(msgText);
//        alertMsg.setSubtitleLocKey("sub-loc-key1");
//        alertMsg.addSubtitleLocArgs("sub-loc-arg1");
//        alertMsg.addSubtitleLocArgs("sub-loc-arg2");
        return alertMsg;
    }

}
